package com.lin.netty.chat.protocol;

import com.lin.netty.chat.Config;
import com.lin.netty.chat.message.LoginRequestMessage;
import com.lin.netty.chat.message.LoginResponseMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LoggingHandler;

import java.util.ArrayList;

public class TestMessageCodec {
    public static void main(String[] args) throws Exception {
        EmbeddedChannel channel = new EmbeddedChannel(
                new LoggingHandler(),
                new LengthFieldBasedFrameDecoder(1024, 11, 4, 1, 0),
                new MessageCodec()
        );

//        channel.writeOutbound(new LoginRequestMessage("zhang", "123"));
        ArrayList<Object> outList = new ArrayList<>();
        new MessageCodec().encode(null, new LoginResponseMessage(true, "reson"), outList);

        ByteBuf byteBuf = (ByteBuf) outList.get(0);
        // 模拟半包
        int length = byteBuf.readableBytes();
        ByteBuf b1 = byteBuf.slice(0, 32);
        ByteBuf b2 = byteBuf.slice(32, length - 32);
        b1.retain();
        channel.writeInbound(b1);
        channel.writeInbound(b2);
    }
}
